package com.hifar.test.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.plat.baseModal.pojo.BaseOssFiles;
import com.hifar.plat.baseModal.service.IBaseOssFilesService;
import com.hifar.test.entrust.constant.TestExceptionConsts;
import com.hifar.test.test.mapper.HfEnvTestExceptionMapper;
import com.hifar.test.test.pojo.HfEnvTest;
import com.hifar.test.test.pojo.HfEnvTestException;
import com.hifar.test.test.pojo.VO.HfEnvTestExceptionSaveVO;
import com.hifar.test.test.service.IHfEnvTestExceptionService;
import com.hifar.test.test.service.IHfEnvTestService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zhuWeiWei
 * @description 异常记录
 * @date 2024/12/23 16:53
 */
@Service
//@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class HfEnvTestExceptionServiceImpl extends ServiceImpl<HfEnvTestExceptionMapper, HfEnvTestException> implements IHfEnvTestExceptionService {
    @Autowired
    private IHfEnvTestService testService;
    @Autowired
    private IBaseOssFilesService baseOssFilesService;


    @Override
    public Integer queryTestExceptionNum(String testId) {
        QueryWrapper<HfEnvTestException> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfEnvTestException::getTestId, testId);
        return this.count(queryWrapper);
    }

    @Override
    public void saveException(HfEnvTestExceptionSaveVO testExceptionSaveVO) {
        String testId = testExceptionSaveVO.getTestId();
        HfEnvTest envTest = testService.getById(testId);
        Assert.notNull(envTest, "数据不存在!");

        HfEnvTestException hfEnvTestException = new HfEnvTestException();

        BeanUtils.copyProperties(testExceptionSaveVO, hfEnvTestException);

        String status = envTest.getStatus();
        hfEnvTestException.setStatus("1");
        hfEnvTestException.setTestStatus(status);
        this.save(hfEnvTestException);
        String id = hfEnvTestException.getId();

        List<BaseOssFiles> fileList = Optional.ofNullable(testExceptionSaveVO.getFiles()).orElse(new ArrayList<>(0));
        for (BaseOssFiles baseOssFiles : fileList) {
            baseOssFiles.setRefId(id);
            baseOssFiles.setRefType(TestExceptionConsts.TEST_EXCEPTION_REF_TYPE_FILE);
        }

        UpdateWrapper<BaseOssFiles> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(BaseOssFiles::getRefId, null)
                .eq(BaseOssFiles::getRefId, id)
                .eq(BaseOssFiles::getRefType
                        , TestExceptionConsts.TEST_EXCEPTION_REF_TYPE_FILE);
        baseOssFilesService.update(updateWrapper);
        baseOssFilesService.updateBatchById(fileList);
    }

    @Override
    public void editException(HfEnvTestExceptionSaveVO testExceptionSaveVO) {
        String testId = testExceptionSaveVO.getTestId();
        HfEnvTest envTest = testService.getById(testId);
        Assert.notNull(envTest, "数据不存在!");

        HfEnvTestException hfEnvTestException = new HfEnvTestException();

        BeanUtils.copyProperties(testExceptionSaveVO, hfEnvTestException);

        String status = envTest.getStatus();
        hfEnvTestException.setStatus("1");
        hfEnvTestException.setTestStatus(status);
        this.updateById(hfEnvTestException);
        String id = hfEnvTestException.getId();

        List<BaseOssFiles> fileList = Optional.ofNullable(testExceptionSaveVO.getFiles()).orElse(new ArrayList<>(0));
        for (BaseOssFiles baseOssFiles : fileList) {
            baseOssFiles.setRefId(id);
            baseOssFiles.setRefType(TestExceptionConsts.TEST_EXCEPTION_REF_TYPE_FILE);
        }

        UpdateWrapper<BaseOssFiles> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(BaseOssFiles::getRefId, null)
                .eq(BaseOssFiles::getRefId, id)
                .eq(BaseOssFiles::getRefType
                        , TestExceptionConsts.TEST_EXCEPTION_REF_TYPE_FILE);
        baseOssFilesService.update(updateWrapper);
        baseOssFilesService.updateBatchById(fileList);
    }
}
